﻿using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace VlcPlayer
{
    public partial class Settings : PhoneApplicationPage
    {
        private List<VlcSettings> _vlcServers;
        private VlcSettings _server;

        private List<VlcSettings> VlcServers
        {
            get { return _vlcServers; }
            set { _vlcServers = value; }
        }

        public Settings()
        {
            InitializeComponent();

            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).Text = AppResources.OK;
            ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).Text = AppResources.Delete;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            var idStr = NavigationContext.QueryString["id"];
            var id = int.Parse(idStr);

            VlcServers = (List<VlcSettings>)IsolatedStorageSettings.ApplicationSettings["Servers"];
            DataContext = _server = VlcServers[id];
        }

        protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            base.OnNavigatingFrom(e);

            IsolatedStorageSettings.ApplicationSettings["Servers"] = VlcServers;
            IsolatedStorageSettings.ApplicationSettings.Save();
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            NavigationService.GoBack();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri(@"/StreamingClient;component/StreamManagment/Settings.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            VlcServers.Remove(_server);
            NavigationService.GoBack();
        }
    }
}